package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.TppDifferenceDao;
import com.qdlc.p2p.dal.dto.TppDifference;
import com.qdlc.p2p.dal.model.TppDifferenceModel;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * 用户托管资金差额 dao
 * @author ygw
 * @version 2.0
 * @date 2015年11月5日 下午6:00:56
 */
@Repository("tppDifferenceDao")
public class TppDifferenceDaoDaoImpl extends BaseDaoImpl<TppDifference> implements TppDifferenceDao {
	
	@Override
	public PageDataList<TppDifferenceModel> list(TppDifferenceModel model) {
		QueryParam param = QueryParam.getInstance();
		if(!StringUtil.isBlank(model.getSearchName())){
			SearchFilter orFilter1 = new SearchFilter("user.userName", Operators.LIKE, model.getSearchName().trim());
    		SearchFilter orFilter2 = new SearchFilter("user.realName", Operators.LIKE, model.getSearchName().trim());
    		param.addOrFilter(orFilter1,orFilter2);
		}else{
			if (StringUtil.isNotBlank(model.getUsername())) {
				param.addParam("user.userName", Operators.LIKE, model.getUsername().trim());
			}
			if (StringUtil.isNotBlank(model.getRealname())) {
				param.addParam("user.realName", Operators.LIKE, model.getRealname().trim());
			}
		}
		if (model.getStatus() == 1) {
			param.addParam("status", 0);
		}
		if (model.getStatus() == 2) {
			param.addParam("status", 1);
		}
		param.addPage(model.getPage(), model.getRows());
		if (model.getOrder().equals("desc")) {
			param.addOrder(OrderType.DESC, model.getSort());
		} else {
			param.addOrder(OrderType.ASC, model.getSort());
		}
		PageDataList<TppDifference> pageDateList = findPageList(param);
		PageDataList<TppDifferenceModel> pageDateList_ = new PageDataList<TppDifferenceModel>();
		List<TppDifferenceModel> list = new ArrayList<TppDifferenceModel>();
		pageDateList_.setPage(pageDateList.getPage());
		if (pageDateList.getList().size() > 0) {
			for (int i = 0; i < pageDateList.getList().size(); i++) {
				TppDifference tppDifference = (TppDifference) pageDateList.getList().get(i);
				TppDifferenceModel am = TppDifferenceModel.instance(tppDifference);
				if(tppDifference.getUser() != null){
    				am.setUsername(tppDifference.getUser().getUserName());
    				am.setRealname(tppDifference.getUser().getRealName());
    				String tppUserCustId = UserUtils.getUserTppCustId(tppDifference.getUser().getUserId());
    				if(StringUtil.isNotBlank(tppUserCustId)){
    					am.setTppUserCustId(tppUserCustId);
    				}
				}
				list.add(am);
			}
		}
		pageDateList_.setList(list);
		return pageDateList_;
	}

    /**
     * 更新状态为已处理
     *
     */
    @Override
    public int updateStatus() {
        String jpql = "update TppDifference set status = 1 where status = 0 ";
        Query query = em.createQuery(jpql);
        return query.executeUpdate();
    }
}
